﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.ArcGIS;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.MapSheet
{
    /// <summary>
    /// 自定义图幅创建对象
    /// </summary>
    public class UserMapsheet
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public UserMapsheet()
        {
            UnitValue = 1000;
            if (SheetHelper.Config != null)
            {
                UnitValue = SheetHelper.Config.UnitValue;
                SizeString = SheetHelper.Config.CurrentSize;
            }
            XDiff = 250;
            YDiff = 250;            
        }

        /// <summary>
        /// 
        /// </summary>
        private double _Scale = 500;

        /// <summary>
        /// 
        /// </summary>
        public double Scale
        {
            get { return _Scale; }
            set
            {
                _Scale = value;
                if (SheetHelper.Config != null && SheetHelper.Config.ScaleTypes != null && SheetHelper.Config.ScaleTypes.Count > 0)
                {
                    SheetScale ss = SheetHelper.Config.ScaleTypes.FirstOrDefault(t => t.Value == value);
                    if (ss == null) return;
                    XOffset = ss.XOffset;
                    YOffset = ss.YOffset;
                }
            }
        }

        /// <summary>
        /// 单位值：1000米；100米；10米； 1米
        /// </summary>
        public int UnitValue { get; set; }

        /// <summary>
        /// x方向500图幅的差
        /// </summary>
        protected double XDiff = 250;

        /// <summary>
        /// y方向500图幅的差
        /// </summary>
        protected double YDiff = 250;

        /// <summary>
        /// 获取或设置图幅大小字符串
        /// </summary>
        public string SizeString
        {
            get
            {
                return XDiff.ToString() + "X" + YDiff.ToString();
            }
            set
            {
                string[] vs = value.ToUpper().Split('X');
                XDiff = double.Parse(vs[0]);
                YDiff = double.Parse(vs[1]);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public double XOffset { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public double YOffset { get; set; }
        
        /// <summary>
        /// 进度条
        /// </summary>
        public Progressor _Progressor { get; set; }
        
        /// <summary>
        /// 图幅宽度
        /// </summary>
        public double Width { get; set; }

        /// <summary>
        /// 图幅高度
        /// </summary>
        public double Height { get; set; }  
      
        /// <summary>
        /// 获取横向图幅的总数
        /// </summary>
        public int ColCount { get; protected set; }

        /// <summary>
        /// 获取纵向图幅的总数
        /// </summary>
        public int RowCount { get; protected set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="geo"></param>
        /// <returns></returns>
        public List<Mapsheet> CreateMapsheet(IGeometry geo)
        {
            List<Mapsheet> sheets = new List<Mapsheet>();
            IRelationalOperator relOp = geo as IRelationalOperator;
            Width = XDiff * Scale / 500;
            Height = YDiff * Scale / 500;
            ColCount = (int)Math.Ceiling((geo.Envelope.XMax - geo.Envelope.XMin) * 1.0 / Width);
            RowCount = (int)Math.Ceiling((geo.Envelope.YMax - geo.Envelope.YMin) * 1.0 / Height);
            if (_Progressor != null) _Progressor.SetMaxValue(ColCount * RowCount);
            for (double x = geo.Envelope.XMin; x < geo.Envelope.XMax + Width; x += Width)
            {
                for (double y = geo.Envelope.YMin; y < geo.Envelope.YMax + Height; y += Height)
                {
                    if (_Progressor != null) _Progressor.Step();
                    string mapno = GetFormatCode(x, y);
                    IPolygon polygon = GeometryHelper.ExtentToPolygon(x, y, x + Width, y + Height);
                    if (relOp.Disjoint(polygon)) continue;
                    Mapsheet sheet = new Mapsheet()
                    {
                        Code = mapno,
                        Checked = true,
                        Geometry = polygon
                    };
                    if (geo.SpatialReference != null) sheet.Geometry.SpatialReference = geo.SpatialReference;
                    List<string> codes = new List<string>();
                    codes.Add(string.Format("{0}:{1}", SheetHelper.F_LeftTop, GetFormatCode(x - Width, y + Height)));
                    codes.Add(string.Format("{0}:{1}", SheetHelper.F_TopIn, GetFormatCode(x, y + Height)));
                    codes.Add(string.Format("{0}:{1}", SheetHelper.F_RightTop, GetFormatCode(x + Width, y + Height)));
                    codes.Add(string.Format("{0}:{1}", SheetHelper.F_LeftIn, GetFormatCode(x - Width, y)));
                    //codes.Add(string.Format("{0}:{1}", SheetHelper.F_TopIn, GetFormatCode(x, y )));
                    codes.Add(string.Format("{0}:{1}", SheetHelper.F_RightTop, GetFormatCode(x + Width, y)));
                    codes.Add(string.Format("{0}:{1}", SheetHelper.F_LeftUnder, GetFormatCode(x - Width, y - Height)));
                    codes.Add(string.Format("{0}:{1}", SheetHelper.F_UnderIn, GetFormatCode(x, y - Height)));
                    codes.Add(string.Format("{0}:{1}", SheetHelper.F_RightUnder, GetFormatCode(x + Width, y - Height)));
                    sheet.UserData = codes;
                    sheets.Add(sheet);
                }
            }
            if (_Progressor != null) _Progressor.Stop();
            return sheets;
        }

        /// <summary>
        /// 根据图号格式获取图号
        /// </summary>
        /// <param name="x">左下角x坐标</param>
        /// <param name="y">左下角y坐标</param>
        /// <returns></returns>
        protected string GetFormatCode(double x, double y)
        {
            string xStr = Math.Round(((x - XOffset) / UnitValue), 2).ToString();
            string yStr = Math.Round(((y - YOffset) / UnitValue), 2).ToString();
            return yStr + "-" + xStr;
        }

    }
}
